
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link href="/css/login.css" rel="stylesheet" type="text/css" >
</head>

<body>

<form id="myForm" action="../action/loginAction.php" method="post">
    <div class="body_1">
        <div class="mains">
            <div class="top">
                <span>图书管理系统</span>
            </div>
            <div class="center">
                <div class="yonghu">
                    用户名 <input type="text" name="username">
                    <span id="message"></span>
                </div>
                <div class="mima">
                    密&nbsp;&nbsp;&nbsp;码 <input type="password" name="password" >
                </div>
                <div class="denglu">
                    <input id="submit" type="submit" value="登录">
                    <input type="button" onclick="history.go(-1)" value="返回">
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    /**
     * Created by Administrator on 2017/4/18.
     */

    var submitButton=document.getElementById('submit');
    var messageElement=document.getElementById('message');
    var myFormElement=document.getElementById('myForm');
    submitButton.onclick=function(e){
        e.preventDefault();
        var elementUsername=document.querySelector('#myForm input[name=username]');
        var username=elementUsername.value;
        if(username == "") {
            messageElement.innerHTML='用户不能为空';
            return;
        };
        var passwordElement=document.querySelector('#myForm input[name=password]');
        var password=passwordElement.value;
        if(password=="") {
            messageElement.innerHTML='密码不能为空';
            return;
        };
        var xhr=new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if(this.readyState == 4){
                if(this.status==200){
                    var result=JSON.parse(this.responseText);
                    console.log(result);
                    if(result.error==1){
                        messageElement.innerHTML=result.message;
                    }else{
                        window.location.href='../index.php';
                    }
                }else {
                    alert("异常");
                }
            }
        };
        xhr.open(myFormElement.method,myFormElement.action);
        var data = new FormData();
        data.append('username', username);
        data.append('password', password);
        console.log(data);
        xhr.send(data);


    };


</script>


</body>
</html>
